/*
 *  Author : Patrick 
 * 
 */

$(function() {
    var successMsgContainer = $("div[id*=successMessageContainer]");
    var errorMsgContainer = $("div[id*=errorMessageContainer]");
    var successMsg = $("span[id*=successMessage]");
    var errorMsg = $("span[id*=errorMessage]");
    //var buttonEnrollStudents = $("button[id*=enrollStudents]");
    // buttonEnrollStudents.click(setSelectedStudentsValues);

    var $studentsGrid = $("#studentsGrid");

    if (successMsg.text())
        successMsgContainer.show();
    if (errorMsg.text())
        errorMsgContainer.show();



    loadStudents();

    $studentsGrid.bind('rowselect', function(event) {

        setSelectedStudentsValues();

        /*var rowindex = event.args.rowindex;
         
         if (rowindex > -1) {
         buttonEnrollStudents.removeClass("disabled");
         buttonDelete.removeClass("disabled");
         buttonEdit.removeAttr("disabled");
         buttonDelete.removeAttr("disabled");
         }
         else {
         buttonEdit.class("disabled");
         buttonDelete.class("disabled");
         buttonEdit.attr("disabled", "disabled");
         buttonDelete.attr("disabled", "disabled");
         }*/
    });

    function setSelectedStudentsValues() {
        var rowindex = $studentsGrid.jqxGrid('getselectedrowindex');
        var datarow = $studentsGrid.jqxGrid('getrowdata', rowindex);

        var frmOfStudGrid = document.getElementById("frmForStudentsGrid");

        //create new hidden input for inputUserId
        var inputUserId = document.createElement("input");
        inputUserId.setAttribute("name", "inputUserId");
        inputUserId.setAttribute("type", "hidden");
        inputUserId.setAttribute("value", datarow.UserId);
         
        //append the created hidden inputs to the form 
        frmOfStudGrid.appendChild(inputUserId);
               
    }

    function loadStudents() {
        // prepare the data
        var source =
                {
                    datatype: "json",
                    datafields: [
                        {name: 'UserId', type: 'string'},
                        {name: 'FirstName', type: 'string'},
                        {name: 'LastName', type: 'string'},
                        {name: 'Email', type: 'string'},
                        {name: 'Username', type: 'string'}
                    ],
                    data: {action: "loadStudents"},
                    url: "./adminEnroll"
                };

        var dataAdapter = new $.jqx.dataAdapter(source,
                {
                    formatData: function(data) {
                        $.extend(data, {
                            featureClass: "P",
                            style: "full",
                            maxRows: 50
                        });

                        return data;
                    }
                }
        );

        var dataAdapter = new $.jqx.dataAdapter(source, {
            downloadComplete: function(data, status, xhr) {
            },
            loadComplete: function(data) {
                return $.makeArray(data);
            },
            loadError: function(xhr, status, error) {
            }
        });

        // initialize jqxGrid
        $studentsGrid.jqxGrid(
                {
                    width: 700,
                    pagesizeoptions: ['10', '20', '50'],
                    source: dataAdapter,
                    theme: 'base',
                    pageable: true,
                    autoshowfiltericon: true,
                    filterable: true,
                    autoheight: true,
                    sortable: true,
                    altrows: true,
                    enabletooltips: true,
                    selectionmode: 'checkbox',
                    columns: [
             
                        {text: 'User ID', datafield: 'UserId'},
                        {text: 'First Name', datafield: 'FirstName'},
                        {text: 'Last Name', datafield: 'LastName'},
                        {text: 'Email', datafield: 'Email'},
                        {text: 'Username', datafield: 'Username'}

                    ]
                });
    }
});
